//Design By Do Trung --- Email : trungqn19852003@yahoo.com
//Ngày Design :29/10/2011 3:17:01 PM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using SoTaiChinh.Library.Common.SqlServer;
using Entity = SoTaiChinh.Library.Entity;
namespace SoTaiChinh.Library.Data
{
	/// <summary>
	/// </summary>
	public class Video
{
		public Video() {}
				Connection db = new Connection();

/// <summary>
/// Inserts a record into the Video table.
/// <summary>
/// <param name="IdVideo"></param>
/// <param name="IdChuDe"></param>
/// <param name="TenFile"></param>
/// <param name="HinhAnhMoTa"></param>
/// <param name="GioiThieu"></param>
/// <param name="NgayDang"></param>
/// <param name="LuotXem"></param>
/// <param name="TrangThai"></param>
/// <returns></returns>
	public bool Insert(Entity.Video entity)
	{
		bool boolInsert = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_Video_Insert";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			//SqlCmd.Parameters.Add("@IdVideo", SqlDbType.Int).Value=entity.IdVideo;
			SqlCmd.Parameters.Add("@IdChuDe", SqlDbType.Int).Value=entity.IdChuDe;
			SqlCmd.Parameters.Add("@TenFile", SqlDbType.NVarChar,400).Value=entity.TenFile;
			SqlCmd.Parameters.Add("@HinhAnhMoTa", SqlDbType.NVarChar,250).Value=entity.HinhAnhMoTa;
			SqlCmd.Parameters.Add("@GioiThieu", SqlDbType.NVarChar,250).Value=entity.GioiThieu;
			SqlCmd.Parameters.Add("@NgayDang", SqlDbType.DateTime).Value=entity.NgayDang;
			SqlCmd.Parameters.Add("@LuotXem", SqlDbType.Int).Value=entity.LuotXem;
            SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

			SqlAdp.InsertCommand = SqlCmd;
			SqlAdp.InsertCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolInsert = true;
			SqlCnn.Close();
		}
		catch
		{
			boolInsert = false;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolInsert;
	}

/// <summary>
/// Selects a single record from the Video table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable Select()
		{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_Video_Select";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}

/// <summary>
/// Selects a single record from the Video table.
/// <summary>
/// <returns>DataTable</returns>
	public DataTable SelectPrimaryKey(Entity.Video entity)
	{
		DataTable dt = new DataTable();
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_Video_SelectPrimaryKey";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdVideo", SqlDbType.Int).Value=entity.IdVideo;

			SqlAdp.SelectCommand = SqlCmd;
			SqlAdp.SelectCommand.ExecuteNonQuery();
			SqlAdp.Fill(dt);
			SqlCmd.Transaction.Commit();
			SqlCnn.Close();
		}
		catch
		{
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return dt;
	}
    public DataTable SelectTimKiem(Entity.Video entity)
    {
        DataTable dt = new DataTable();
        SqlConnection SqlCnn = db.ReadFromWebConfig();
        SqlDataAdapter SqlAdp = new SqlDataAdapter();
        SqlCommand SqlCmd = new SqlCommand();
        SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
        try
        {
            SqlCmd.Transaction = SqlTrans;
            SqlCmd.Connection = SqlCnn;
            SqlCmd.CommandText = "Proc_Video_TimKiem";
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.Parameters.Add("@IdChuDe", SqlDbType.Int).Value = entity.IdChuDe;
            SqlCmd.Parameters.Add("@Option", SqlDbType.Int).Value = entity.Option;
            SqlCmd.Parameters.Add("@KeySearch", SqlDbType.NVarChar, 100).Value = entity.TenFile;
            SqlAdp.SelectCommand = SqlCmd;
            SqlAdp.SelectCommand.ExecuteNonQuery();
            SqlAdp.Fill(dt);
            SqlCmd.Transaction.Commit();
            SqlCnn.Close();
        }
        catch
        {
            SqlCmd.Transaction.Rollback();
        }
        finally
        {
            SqlCmd.Parameters.Clear();
            SqlTrans = null;
        }
        if (SqlCmd != null)
            SqlCmd.Dispose();
        if (SqlAdp != null)
            SqlAdp.Dispose();
        return dt;
    }
/// <summary>
/// Updates a record in the Video table.
/// <summary>
/// <param name="IdVideo"></param>
/// <param name="IdChuDe"></param>
/// <param name="TenFile"></param>
/// <param name="HinhAnhMoTa"></param>
/// <param name="GioiThieu"></param>
/// <param name="NgayDang"></param>
/// <param name="LuotXem"></param>
/// <param name="TrangThai"></param>
	public bool Update(Entity.Video entity)
	{
		bool boolUpdate = false ;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_Video_Update";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdVideo", SqlDbType.Int).Value=entity.IdVideo;
			SqlCmd.Parameters.Add("@IdChuDe", SqlDbType.Int).Value=entity.IdChuDe;
			SqlCmd.Parameters.Add("@TenFile", SqlDbType.NVarChar,400).Value=entity.TenFile;
			SqlCmd.Parameters.Add("@HinhAnhMoTa", SqlDbType.NVarChar,250).Value=entity.HinhAnhMoTa;
			SqlCmd.Parameters.Add("@GioiThieu", SqlDbType.NVarChar,250).Value=entity.GioiThieu;
			SqlCmd.Parameters.Add("@NgayDang", SqlDbType.DateTime).Value=entity.NgayDang;
			SqlCmd.Parameters.Add("@LuotXem", SqlDbType.Int).Value=entity.LuotXem;
            SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

			SqlAdp.UpdateCommand = SqlCmd;
			SqlAdp.UpdateCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolUpdate = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolUpdate = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolUpdate;
	}
    public bool Update_TrangThai(Entity.Video entity)
    {
        bool boolUpdate = false;
        SqlConnection SqlCnn = db.ReadFromWebConfig();
        SqlDataAdapter SqlAdp = new SqlDataAdapter();
        SqlCommand SqlCmd = new SqlCommand();
        SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
        try
        {
            SqlCmd.Transaction = SqlTrans;
            SqlCmd.Connection = SqlCnn;
            SqlCmd.CommandText = "Proc_Video_Update_TrangThai";
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.Parameters.Add("@IdVideo", SqlDbType.Int).Value = entity.IdVideo;
            SqlAdp.UpdateCommand = SqlCmd;
            SqlAdp.UpdateCommand.ExecuteNonQuery();
            SqlCmd.Transaction.Commit();
            boolUpdate = true;
            SqlCnn.Close();
        }
        catch
        {
            boolUpdate = false;
            SqlCmd.Transaction.Rollback();
        }
        finally
        {
            SqlCmd.Parameters.Clear();
            SqlTrans = null;
        }
        if (SqlCmd != null)
            SqlCmd.Dispose();
        if (SqlAdp != null)
            SqlAdp.Dispose();
        return boolUpdate;
    }
/// <summary>
		/// Deletes a record from the Video table by a composite primary key.
/// <summary>
	public bool Delete(Entity.Video entity)
	{
		bool boolDelete = false;
		SqlConnection SqlCnn = db.ReadFromWebConfig();
		SqlDataAdapter SqlAdp = new SqlDataAdapter();
		SqlCommand SqlCmd = new SqlCommand();
		SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
		try
		{
			SqlCmd.Transaction = SqlTrans;
			SqlCmd.Connection = SqlCnn;
			SqlCmd.CommandText="Proc_Video_Delete";
			SqlCmd.CommandType=CommandType.StoredProcedure;
			SqlCmd.Parameters.Add("@IdVideo", SqlDbType.Int).Value=entity.IdVideo;

			SqlAdp.DeleteCommand = SqlCmd;
			SqlAdp.DeleteCommand.ExecuteNonQuery();
			SqlCmd.Transaction.Commit();
			boolDelete = true ;
			SqlCnn.Close();
		}
		catch
		{
			boolDelete = false ;
			SqlCmd.Transaction.Rollback();
		}
		finally
		{
			SqlCmd.Parameters.Clear();
			SqlTrans = null;
		}
		if (SqlCmd != null)
			SqlCmd.Dispose();
		if (SqlAdp != null)
			SqlAdp.Dispose();
		return boolDelete;
	}
    
}
}
